//
//  FKHomeHotCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/13.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHomeHotCell.h"
#import "FKHomeGoods.h"

@interface FKHomeHotCell ()

@property (nonatomic, weak) UIImageView *pictureView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *inventoryLabel;
@property (nonatomic, weak) UIButton *saleBtn;
@property (nonatomic, weak) UILabel *sharePriceLabel;
@property (nonatomic, weak) UIButton *promotionBtn;
@property (nonatomic, weak) UILabel *userPriceLabel;
@property (nonatomic, weak) UILabel *memberPriceLabel;

@end

@implementation FKHomeHotCell

- (void)setupSubviews
{
    [super setupSubviews];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIImageView *pictureView = [[UIImageView alloc] init];
    [self.contentView addSubview:pictureView];
    self.pictureView = pictureView;
    [pictureView fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIButton *saleBtn = [UIButton fk_btnWithTarget:nil action:nil font:fk_adjustFont(10) normalTitle:nil normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"home_sales_bg"] selectedBgImg:nil];
    [self.contentView addSubview:saleBtn];
    self.saleBtn = saleBtn;
    saleBtn.userInteractionEnabled = false;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:[UIColor blackColor]];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *inventoryLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:inventoryLabel];
    self.inventoryLabel = inventoryLabel;
    
    // 普通用户显示的价格
    UILabel *userPriceLabel = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:fkPriceRed];
    [self.contentView addSubview:userPriceLabel];
    self.userPriceLabel = userPriceLabel;
    
    // 会员显示的价格
    UILabel *memberPriceLabel = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:fkPriceRed];
    [self.contentView addSubview:memberPriceLabel];
    self.memberPriceLabel = memberPriceLabel;
    
    UILabel *sharePriceLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkPriceRed];
    [self.contentView addSubview:sharePriceLabel];
    self.sharePriceLabel = sharePriceLabel;
    
    UIButton *promotionBtn = [UIButton fk_btnWithTarget:self action:@selector(promotionBtnClicked:) font:fk_adjustFont(11) normalTitle:@"立即推广" normalTitleColor:fkPriceRed selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self.contentView addSubview:promotionBtn];
    self.promotionBtn = promotionBtn;
    [promotionBtn fk_viewCornerRadius:5 borderWidth:1 borderColor:fkPriceRed];
    
    CGFloat picW = (fkScreenW - 24 - fk_adjustW(10)) * 0.5;
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(picW);
        make.top.left.offset(0);
    }];
    [saleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(pictureView);
        make.height.mas_equalTo(fk_adjustW(18));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pictureView.mas_bottom).offset(fk_adjustW(12));
        make.left.offset(6);
        make.right.offset(-6);
    }];
    [userPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.offset(fk_adjustW(-8));
    }];
    [inventoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        //        make.top.equalTo(titleLabel.mas_bottom).offset(fk_adjustW(10));
        make.bottom.equalTo(userPriceLabel.mas_top).offset(fk_adjustW(-4));
    }];
    [sharePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(pictureView).offset(fk_adjustW(-10));
        make.bottom.equalTo(userPriceLabel);
        make.left.equalTo(titleLabel);
    }];
    [memberPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.equalTo(sharePriceLabel.mas_top).offset(fk_adjustW(-6));
    }];
    [promotionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(fk_adjustW(55));
        make.height.mas_equalTo(fk_adjustW(20));
        make.right.equalTo(titleLabel);
//        make.bottom.equalTo(sharePriceLabel);
        make.bottom.offset(fk_adjustW(-10));
    }];
}

- (void)promotionBtnClicked:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(homeCell:didClickedPromotionBtn:)]) {
        [self.delegate homeCell:self didClickedPromotionBtn:btn];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[FKHomeGoods class]]) {
        FKHomeGoods *model = data;
        
        [self.pictureView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:kPlaceholder_1x1];
        [self.saleBtn setTitle:fkFORMAT(@"已售%@件 | %@", model.sales, model.name) forState:UIControlStateNormal];
        
        self.titleLabel.text = model.title;
        self.inventoryLabel.text = fkFORMAT(@"库存%@件", model.inventory);
        self.userPriceLabel.attributedText = model.hotPriceAttr;
        self.memberPriceLabel.attributedText = model.hotPriceAttr;
        self.sharePriceLabel.attributedText = model.memberSharePriceAttr;
        
        if (model.isMember) {
            self.userPriceLabel.hidden = true;
            self.inventoryLabel.hidden = true;
            self.memberPriceLabel.hidden = false;
            self.sharePriceLabel.hidden = false;
            [self.promotionBtn setTitle:@"立即推广" forState:UIControlStateNormal];
        } else {
            self.userPriceLabel.hidden = false;
            self.inventoryLabel.hidden = false;
            self.memberPriceLabel.hidden = true;
            self.sharePriceLabel.hidden = true;
            [self.promotionBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        }
    }
}

@end
